home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / prog / strpp212.zip / STR.DOC < prev    next >
Text File  |  1993-02-17  |  14KB  |  367 lines

  1.  
  2.                            String++ Version 2.12
  3.  
  4.                       Copyright 1992 by Carl Moreland
  5.                                  02/16/93
  6.  
  7. ---------------------------------------------------------------------------
  8.  
  9.     String++  is a string class written for Borland/Turbo C++.  To use  the
  10. string  class in your program,  simply add str.lib to your project or  make
  11. file and #include str.h in any module that uses a string.  str.lib contains
  12. all the functions in str.cpp,  but the functions are separated into differ-
  13. ent  object modules for greater efficiency.  Note that str.lib is  compiled
  14. for the large memory model.
  15.  
  16.     Strings may be declared or initialized in the following ways:
  17.  
  18.         string str;                     // NULL string
  19.                str('H');                // single character
  20.                str = 'H';               // single character
  21.                str(ch);                 // single character ch
  22.                str = ch;                // single character ch
  23.                str(ch, 5);              // make 5 copies of ch
  24.                str("Hello World");      // char*
  25.                str = "Hello World";     // char*
  26.                str(ptr);                // char *ptr
  27.                str = ptr;               // char *ptr
  28.                str(ptr, 5);             // begin at ptr[5]
  29.                str(ptr, 5, 3);          // begin at ptr[5], use 3 chars
  30.                str(str1);               // another string
  31.                str = str1;              // another string
  32.                str(str1, 5);            // begin at str1[5]
  33.                str(str1, 5, 3);         // begin at str1[5], use 3 chars
  34.                str(45);          // convert an int to a string
  35.  
  36. Unlike  many  other string classes,  this class does  not  utilize  minimum
  37. string buffer sizes, such as 10 characters. Minimum length strings have the
  38. advantage of reduced memory management if the manipulated string is  always
  39. less than the minimum size;  however, the penalty is that every string will
  40. be  at  least the minimum size.  As always,  it's a tradeoff of  speed  vs.
  41. memory.
  42.  
  43. The following operators are available:
  44.  
  45.         =       see above
  46.         ()      str() returns the char* of the string
  47.         []      str[i] returns the ith character of str; [] can also assign
  48.                 the ith character of a string.
  49.         +  +=   concatenates
  50.         *  *=   multiple duplicates
  51.         == !=   comparisons
  52.         <  >    comparisons
  53.         <= >=   comparisons
  54.  
  55. There is also a cast operator which returns type const char*. This allows a
  56. string  object to be directly used as a parameter in most C and  C++  func-
  57. tions without having to use the () operator:
  58.  
  59.     char *p;
  60.     strcpy(p, s1);        // where s1 is a string
  61.  
  62. The "+" operator and all comparison operators are declared as friend  func-
  63. tions.  This enables them to accept char, char*, and string as valid  argu-
  64. ments  on either side of the operator.  Therefore, each of these statements
  65. is valid:
  66.  
  67.         str1 = "Hello" + str2;
  68.         str1 = str2 + "World";
  69.         str1 = "Hello" + str2 + "World";
  70.         if(str1 == "Hello World") ...
  71.         if("Hello World" == str1) ...
  72.  
  73. Technically,  most  of the operators do not need  overloading  because  the
  74. string  constructors  will  automatically  convert the  arguments  to  type
  75. string.  The advantage is that fewer functions must be coded,  which  saves
  76. memory. The disadvantage is that there is a speed penalty because construc-
  77. tors (might) have to be called to convert arguments.  The class was  tested
  78. both ways and it was found that the added code was small, but the speed was
  79. more than doubled when using overloaded operators. Therefore, the operators
  80. are overloaded to accept string and char*.  There are no overloads for type
  81. char parameters because it would most probably be a rare case, and the con-
  82. structor can pick it up. Besides, the two following statements are entirely
  83. equivalent:
  84.  
  85.         if(str1 == 'a') ...
  86.         if(str1 == "a") ...
  87.  
  88.     Finally, the demo program str_test.cpp contains a suite of string mani-
  89. pulations which demonstrate most of the string functions.
  90.  
  91. ---------------------------------------------------------------------------
  92. ---------------------------------------------------------------------------
  93.  
  94. Class Functions:
  95.  
  96. ---------------------------------------------------------------------------
  97. int Length(void) const;
  98. int Len(void)    const;
  99.  
  100. Both functions return the length of the string:
  101.  
  102.         str = "Hello World";
  103.  
  104.         i = str.Length();               // i = 11
  105.  
  106. ---------------------------------------------------------------------------
  107. string& Left(int len);
  108. string& Right(int len);
  109. string& Mid(int pos, int len);
  110.  
  111. These functions return substrings of a string:
  112.  
  113.         str1 = str2 = str3 = "This is a test";
  114.  
  115.         str1.Left(4);            // str1 = "This"
  116.         str2.Mid(5, 4);            // str2 = "is a"
  117.         str3.Right(4);            // str3 = "test"
  118.  
  119. ---------------------------------------------------------------------------
  120. string& Justify(int mode, int len, int clip=0);
  121.  
  122. This function justifies a string by padding it with spaces (0x20) until  it
  123. is 'len' long.  If the original string had any leading  or  trailing  white
  124. spaces then they are first removed.  If the original string is longer  than
  125. 'len' (after removal of white spaces) then it is clipped to len if  clip=1,
  126. otherwise the trimmed string is returned.
  127.  
  128.         str1 = str2 = str3 = "Hello World";
  129.  
  130.         str1.Justify(LEFT, 20);        // str1 = "Hello World         "
  131.         str2.Justify(CENTER, 20);    // str2 = "    Hello World     "
  132.         str3.Justify(RIGHT, 20);    // str3 = "         Hello World"
  133.  
  134. ---------------------------------------------------------------------------
  135. string& Trim(int mode = CENTER, char ch = WHITESPACE);
  136.  
  137. This function can trim leading or trailing characters from a string. The
  138. trimmed character defaults to WHITESPACE (spaces & tabs) and can be user-
  139. defined.
  140.  
  141.     str1 = str2 = str3 = "    Spaces    ";
  142.  
  143.         str1.Trim(LEFT);        // str1 = "Spaces    "
  144.         str2.Trim(RIGHT);        // str2 = "    Spaces"
  145.         str3.Trim();            // str3 = "Spaces"
  146.  
  147. ---------------------------------------------------------------------------
  148. string& Insert(unsigned pos, const string& s);
  149.  
  150. Inserts a substring at position pos.
  151.  
  152.     str = "This a test";
  153.  
  154.         str.Insert(5, "is ");        // str = "This is a test"
  155.  
  156. ---------------------------------------------------------------------------
  157. string& Delete(unsigned pos, unsigned len = 10000);
  158.  
  159. Deletes a substring beginning at position pos with length len.
  160.  
  161.     str = "This is a test";
  162.  
  163.         str.Delete(5, 3);        // str = "This a test"
  164.  
  165. ---------------------------------------------------------------------------
  166. char* Copy(char*&);
  167.  
  168. Copies  the  string  contents to a non-const character pointer.  Copy  will
  169. allocate the memory for the pointer - it is up to you to free (delete)  it.
  170. Normally, the contents of a string are extracted with the ()operator or the
  171. cast operator.  However,  this returns a const char* that must be cast to a
  172. non-const char* for many C-style functions (most Borland library  functions
  173. will accept const char*).
  174.  
  175.     s1 = "lowercase";
  176.     strupr(s1);        // won't work - strupr() expects char*
  177.         strupr((char*)s1);    // cast to char* - this works
  178.  
  179.     char* p;
  180.         s1.Copy(p);        // copy str to char *p
  181.         strupr(p);        // normal C-style
  182.  
  183. Although  you could also use the ptr() method to return a non-const  char*,
  184. this  is considered unsafe as it completely strips the inherent  protection
  185. that the string class offers.
  186.  
  187. ---------------------------------------------------------------------------
  188. int Index(const string& t);
  189.  
  190. Member method of the AWK function index().
  191.  
  192. ---------------------------------------------------------------------------
  193. string SubStr(unsigned p, unsigned n=10000);
  194.  
  195. Member method of the AWK function substr().
  196.  
  197. ---------------------------------------------------------------------------
  198. int Split(string*& a, const string& fs);
  199.  
  200. Member method of the AWK function split().
  201.  
  202. ---------------------------------------------------------------------------
  203. int Sub(const string& from, const string& to, unsigned count=10000);
  204.  
  205. Member method of the AWK function gsub().
  206.  
  207. ---------------------------------------------------------------------------
  208. string& toUpper(void);
  209. string& toLower(void);
  210.  
  211. These functions convert the string to upper or lower case,  and also return
  212. the converted string.
  213.  
  214.         str1 = "uppercase";
  215.         str2 = "LOWERCASE";
  216.  
  217.         str1.toUpper();                 // str1 is now "UPPERCASE"
  218.         str2.toLower();                 // str2 is now "lowercase"
  219.  
  220. ---------------------------------------------------------------------------
  221. int&  Value(int& n);
  222. long& Value(long& n);
  223.  
  224. Returns the value of a numeric string.
  225.  
  226.         int n1;
  227.     str = "12345";
  228.  
  229.         str.Value(n1);            // n1 is now 12345
  230.  
  231. ---------------------------------------------------------------------------
  232. ---------------------------------------------------------------------------
  233.  
  234. Awk-style Functions:
  235.  
  236. Note: AWK  begins array indexing at 1, whereas C/C++ begins array  indexing
  237.       at 0.  Therefore, most of the implemented AWK functions return values
  238.       that correspond to C-style indexing.  An AWK function that might nor-
  239.       mally return a 0 as a failure indicator will return a -1 here.
  240.  
  241. ---------------------------------------------------------------------------
  242. int length(string& s);
  243.  
  244. Returns the length of a string:
  245.  
  246.         str = "Hello World";
  247.  
  248.         i = length(str);                // i = 11
  249.  
  250. ---------------------------------------------------------------------------
  251. int index(string& s, string& t);
  252.  
  253. Returns the position of 't' in 's' if it exists, or -1 if it doesn't:
  254.  
  255.         str = "d:\\prog\\str"
  256.  
  257.         i = index(str, ":");            // i = 1
  258.  
  259. ---------------------------------------------------------------------------
  260. string substr(string& s, unsigned int p, unsigned int n=10000);
  261.  
  262. Returns the substring of 's' beginning at position 'p' with length 'n':
  263.  
  264.         str1 = "Don't just stand there...";
  265.  
  266.         str2 = substr(str, 11, 5);      // str2 = "stand"
  267.  
  268. ---------------------------------------------------------------------------
  269. int split(string& s, string*& a, string& fs);
  270.  
  271. Splits  string 's' into an array 'a' on field separator 'fs'.  Array 'a' is
  272. normally  declared  as  an uninitialized pointer in  the  calling  function
  273. (string *a),  and a reference to the pointer is passed to split().  split() 
  274. will then allocate memory for the correct array size and return the  number 
  275. of fields:
  276.  
  277.         string *array;
  278.         str = "d:\\prog\\str";
  279.  
  280.         i = split(str, array, "\\";    // i = 3
  281.         // array[0] = "d:"
  282.     // array[1] = "prog"
  283.     // array[2] = "str"
  284.  
  285. ---------------------------------------------------------------------------
  286. int sub(string& from, string& to, string& str);
  287.  
  288. Substitute  'to'  for the leftmost substring of 'str' that  is  matched  by
  289. 'from'. Return the number of substitutions (0 or 1). This function actually
  290. calls gsub() with num=1;
  291.  
  292.         str = "d:\\prog\\str";
  293.  
  294.         i = sub("\\", "/", str);        // i = 1
  295.         // str = "d:/prog\str"
  296.  
  297. ---------------------------------------------------------------------------
  298. int gsub(string& from, string& to, string& str, int num=10000);
  299.  
  300. Substitute 'to' for 'from' globally in string 'str' up to 'num' times.  Re-
  301. turn the number of substitutions.
  302.  
  303.         str = "d:\\prog\\str";
  304.         i = gsub("\\", "/", str);       // i = 2
  305.         // str = "d:/prog/str"
  306.  
  307. ---------------------------------------------------------------------------
  308. ---------------------------------------------------------------------------
  309.  
  310. C-Style Functions:
  311.  
  312. ---------------------------------------------------------------------------
  313. string toupper(string& s);
  314. string tolower(string& s);
  315.  
  316. These  functions return the upper- or lowercase versions of a  string.  The
  317. string itself is not modified.
  318.  
  319.         str1 = "uppercase";
  320.         str2 = "LOWERCASE";
  321.  
  322.         str3 = toupper(str1);           // str3 = "UPPERCASE"
  323.         str4 = tolower(str2);           // str4 = "lowercase"
  324.  
  325. ---------------------------------------------------------------------------
  326. string right(string& s, int n);
  327. string left(string& s, int n);
  328. string mid(const string& s, int p, int n);
  329.  
  330. These functions return substrings of a string:
  331.  
  332.         str = "This is a test";
  333.  
  334.         str1 = left(str, 4);            // str1 = "This"
  335.         str2 = mid(str, 5, 4);          // str2 = "is a"
  336.         str3 = right(str, 4);           // str3 = "test"
  337.  
  338. ---------------------------------------------------------------------------
  339. string justify(string& s, int mode, int len, int clip=0);
  340.  
  341. This function justifies a string by padding it with spaces (0x20) until  it
  342. is 'len' long.  If the original string had any leading  or  trailing  white
  343. spaces then they are first removed.  If the original string is longer  than
  344. 'len' (after removal of white spaces)  then it is clipped to len if clip=1,
  345. otherwise the trimmed string is returned.
  346.  
  347.         str = "Hello World";
  348.  
  349.         str1 = justify(str, LEFT, 20);   // str1 = "Hello World         "
  350.         str2 = justify(str, CENTER, 20); // str2 = "    Hello World     "
  351.         str3 = justify(str, RIGHT, 20);  // str3 = "         Hello World"
  352.  
  353. ---------------------------------------------------------------------------
  354. string trim(string& s, int mode = CENTER, char ch = WHITESPACE);
  355.  
  356. This function can trim leading or trailing characters from a string. The
  357. trimmed character defaults to WHITESPACE (spaces & tabs) and can be user-
  358. defined.
  359.  
  360.     str = "    Spaces    ";
  361.  
  362.         str1 = trim(str, LEFT);        // str1 = "Spaces    "
  363.         str2 = trim(str, RIGHT);    // str2 = "    Spaces"
  364.         str3 = trim(str);        // str3 = "Spaces"
  365.  
  366. ---------------------------------------------------------------------------
  367.